package com.yinhe.music.yhmusic.radio;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RadioListAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.radio.contract.IRadioListContract;
import com.yinhe.music.yhmusic.radio.presenter.RadioListPresenter;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;

/**
 * @author 何晓华
 * @date 2018/5/29
 */

public class MyCollectRadioActivity extends BaseServiceActivity implements BaseQuickAdapter.OnItemClickListener, IRadioListContract.IRadioListView {
    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView recyclerview;

    private RadioListAdapter mAdapter;
    private int mPageNum;

    RadioListPresenter mPresenter;

    @Override
    public void initView() {
        RxBus.get().register(this);
        setToolbar("我的电台");
        showQuickControl(true);

        initAdapter();

        mPage = 1;
        mPresenter.getMyRadioList(mPageSize, mPage);
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_collection_radio;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new RadioListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void initAdapter() {
        mAdapter = new RadioListAdapter();

        mAdapter.setEmptyView(getView(R.layout.include_empty_layout, recyclerview));
        mAdapter.setOnItemClickListener(this);

        mAdapter.setPreLoadNumber(3);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());

        mAdapter.setOnLoadMoreListener(() -> {
            if (mPage <= mPageNum) {
                mPresenter.getMyRadioList(mPageSize, mPage);
            } else {
                mAdapter.loadMoreEnd();
            }
        }, recyclerview);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        recyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerview.setAdapter(mAdapter);
    }

    private void startRadioInfo(RadioListInfo radioInfo) {
        Intent intent = new Intent(this, RadioActivity.class);
        intent.putExtra("radioid", radioInfo.getRadioId());
        startActivity(intent);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        RadioListInfo radioInfo = (RadioListInfo) adapter.getItem(position);
        if (radioInfo != null) {
            startRadioInfo(radioInfo);
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {@Tag(RxBusEventType.Radio.COLLECT_RADIO)}
    )
    public void onRxbusUpdateRadio(String id) {
        mPage = 1;
        mPresenter.getMyRadioList(mPageSize, mPage);
    }

    @Override
    public void setRadioListUI(RadioListInfo radioListInfo) {

        List<RadioListInfo> list = radioListInfo.getRadioList();
        if (list == null) {
            return;
        }
        mPageNum = radioListInfo.getPageNum();

        if (mPage == 1) {
            mAdapter.setEmptyView(getView(R.layout.include_empty_layout, recyclerview));
        }

        recyclerview.stopScroll();
        setLoadMoreList(list, mPageNum, mAdapter);
    }

    @Override
    public void setLoadMoreFailUI() {
        mAdapter.loadMoreFail();
    }
}
